import requests
from lxml import etree
import time
import re
import subprocess

def cook(sj):
    url = 'http://jnzyk.36ve.com/login/sms-login'
    headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
        }

    r = requests.get(url, headers=headers)
    re1 = re.findall('_csrf(.*?)}', r.text.replace(': ', '').replace("'", ""))
    url1 = 'http://jnzyk.36ve.com/login/send-message-code'
    data1 = {
            'tel': sj,
            '_csrf': re1[0]
        }
    requests.post(url1,headers=headers,data=data1)


def yzm(y, sj):
    url2 = 'http://jnzyk.36ve.com/login/sms-login'
    data = {
        'SmsLoginForm[tel]': sj,
        'SmsLoginForm[smscode]': y,
        'ajax': 'login-form-2'
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
        'Host': 'jnzyk.36ve.com',
        'Origin': 'http://jnzyk.36ve.com',
        'Referer': 'http://jnzyk.36ve.com/login/sms-login?url='
    }
    cookie_jar = requests.post(url2, headers=headers, data=data, allow_redirects=False).cookies
    cookie_t = requests.utils.dict_from_cookiejar(cookie_jar)
    cookies = dict(cookie_t)
    return cookies



def Id1():
    #url = 'http://jnzyk.36ve.com/Student/learning-content/index?tcourseId=cbf16b6e-39fc-371c-bf89-fe926a28c9c3'
    url = 'http://jnzyk.36ve.com/Student/learning-content/index?tcourseId=edc72fa3-0297-3270-a4d7-7971aa1bc6e5'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
        , 'Host': 'jnzyk.36ve.com',
        'Cookie':cok
    }
    r = requests.get(url,headers=headers,cookies=cok)
    html = etree.HTML(r.text)
    id = html.xpath('//*[@id="lhMenu"]/ul/li/@onclick')
    name = html.xpath('//*[@id="lhMenu"]/ul/li/div/span[2]/span/text()')
    map1 = {}
    for i,n in zip(id,name):
        map1[n]=i.replace('showSection(','').replace(',event)','').replace("'",'')
    return map1
def Post_id(cok):
    for i,y in Id1().items():
        url = 'http://jnzyk.36ve.com/Student/learning-content/get-section'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
            , 'Host': 'jnzyk.36ve.com',
            'Cookie':cok
        }
        data = {
            'chapter_id': y,
            'find_t':'',
            'course_id': '1a1c4f42-d322-39dc-b54d-59644573431b',

        }
        r = requests.post(url=url,headers=headers,data=data,cookies=cok)

        html = etree.HTML(r.text)
        name = html.xpath('//li/div/a/span/text()')
        if name == []:
            print(i,y)
            ui = html.xpath('//li/ul/@id')
            def get(id):
                try:
                    url = 'http://jnzyk.36ve.com/Student/learning-content/get-section'
                    headers = {
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
                        , 'Host': 'jnzyk.36ve.com',
                        'Cookie': cok
                    }
                    data = {
                        'chapter_id': id,
                        'find_t': '',
                        'course_id': '1a1c4f42-d322-39dc-b54d-59644573431b',
                        '_csrf': 'Nv4vY9g6ZNySEOvRkiyg12AZ6nq_4RDNxY9EXiKnHaRBkR8hu3Iems1onan_WsmSFnTYKe-GKYjxzQIdePZq_g=='
                    }
                    r = requests.post(url=url, headers=headers, data=data,cookies=cok)
                    html = etree.HTML(r.text)
                    #name = html.xpath('//li/div/a/span/text()')
                    Z_url = html.xpath('//li/div/a/@href')
                    jxd  = re.findall('19px;">(.*?)%',r.text)
                    return Z_url,jxd
                except:
                    print('报错了 报错了')
            for x in ui:
                try:
                   # p#rint(get(x.replace('Html_','')))
                    jee1, jex1 = get(x.replace('Html_', ''))
                    for jee,jex in zip(jee1, jex1):
                        print('正常的',jex
                        )
                        if jex == '100':
                            print('被返回的',jex)
                            break
                        re1 = re.findall("resource_id=(.*?)&",jee)
                        re2 = re.findall("tcourseId=(.*?)&", jee)
                        re3 = re.findall("item_id=(.*?)$", jee)
                        Url = 'http://jnzyk.36ve.com/Student/learning-content/paused?rid=' + re1[0] + '&cid=' + re2[
                            0] + '&item_id=' + re3[0] + '&current_play_time=' + str(Mp4_get(jee)) + '&resource_time=0&_csrf='+Csrf(jee)
                        headers = {
                            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
                            , 'Host': 'jnzyk.36ve.com',
                            'Cookie': cok,
                            'Referer': 'http://jnzyk.36ve.com/Student/learning-content/learning?resource_id=46db5384-b6ba-36f4-bf3e-38fe08f5bc50&tcourseId=cbf16b6e-39fc-371c-bf89-fe926a28c9c3&item_id=874cbb26-116a-377a-8619-c2c967665414'
                        }

                        r123=  requests.get(Url, headers=headers,cookies=cok)
                        print(r123,jex)
                except:
                    print('外围报错')

        else:
            print(i,y)
            for ui,uo in zip(html.xpath('//li/div/a/@href'),re.findall('19px;">(.*?)%',r.text)):
                try:
                    re1 = re.findall("resource_id=(.*?)&", ui)
                    re2 = re.findall("tcourseId=(.*?)&", ui)
                    re3 = re.findall("item_id=(.*?)$", ui)
                    if uo == '100':
                        break
                    Url = 'http://jnzyk.36ve.com/Student/learning-content/paused?rid='+re1[0]+'&cid='+re2[0]+'&item_id='+re3[0]+'&current_play_time='+str(Mp4_get(ui))+'&resource_time=0&_csrf='+Csrf(ui)
                    headers = {
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
                        , 'Host': 'jnzyk.36ve.com',
                        'Cookie':cok,
                        'Referer': 'http://jnzyk.36ve.com/Student/learning-content/learning?resource_id=46db5384-b6ba-36f4-bf3e-38fe08f5bc50&tcourseId=cbf16b6e-39fc-371c-bf89-fe926a28c9c3&item_id=874cbb26-116a-377a-8619-c2c967665414'
                    }

                    r23 = requests.get(Url,headers=headers,cookies=cok)

                    print('########')
                    print(r23)
                    #print(r23)
                except:
                    None

def get_length(filename):
    result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
                             "format=duration", "-of",
                             "default=noprint_wrappers=1:nokey=1", filename],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    return float(result.stdout)

def Mp4_get(ui):
    try:
        url ='http://jnzyk.36ve.com'+ui
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
            , 'Host': 'jnzyk.36ve.com',
            'Cookie': cok
        }
        r = requests.get(url,headers=headers,cookies=cok)
        re1 = re.findall("\[\'(.*?)\'",r.text)
        return int(get_length(re1[4]))
    except:
        None
def Csrf(ur):
    url ='http://jnzyk.36ve.com'+ur
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
        , 'Host': 'jnzyk.36ve.com',
       'Cookie': cok
    }
    r = requests.get(url, headers=headers,cookies=cok)
    re1 = re.findall("_csrf':'(.*?)'}", r.text)
    return re1[0]
def Ti(num):
    timeStamp = num
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%M:%S", timeArray)
    return otherStyleTime
if __name__ == '__main__':
    sj1 = input("请输入手机号:")
    cook(sj1)
    yzm1 = input("验证码")
    cok = yzm(yzm1,sj1)
    Post_id(cok)
